#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

double Widget:: evaluateExpression(const QString &expression) {
    // 定义操作数栈
    QStack<double> numbers;
    // 定义操作符栈
    QStack<QChar> operations;

    // 遍历表达式中的每个字符
    for (int i = 0; i < expression.length(); ++i) {
        QChar ch = expression[i];

        if (ch.isDigit() || ch == '.') { // 如果是数字或小数点
            double num = 0;
            int dotCount = 0;
            while ((i < expression.length()) && (expression[i].isDigit() || expression[i] == '.')) {
                if (expression[i] == '.') {
                    if (++dotCount > 1) break; // 处理非法的小数点
                } else {
                    if (dotCount == 0)
                        num = num * 10 + (expression[i].digitValue());
                    else
                        num += (expression[i].digitValue()) * std::pow(10, -dotCount);
                }
                ++i;
            }
            --i; // 回退一位，因为for循环会自动i++
            numbers.push(num);
        } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { // 如果是运算符
            while (!operations.isEmpty() && precedence(ch) <= precedence(operations.top())) {
                performOperation(numbers, operations);
            }
            operations.push(ch);
        } else if (ch == '(') { // 左括号直接压栈
            operations.push(ch);
        } else if (ch == ')') { // 右括号弹出运算符直到遇到左括号
            while (operations.top() != '(') {
                performOperation(numbers, operations);
            }
            operations.pop(); // 弹出左括号
        }
    }

    // 执行剩余的操作
    while (!operations.isEmpty()) {
        performOperation(numbers, operations);
    }

    return numbers.pop();
}

void Widget:: performOperation(QStack<double> &numbers, QStack<QChar> &operations) {
    if (numbers.size() < 2 || operations.isEmpty())
        throw std::runtime_error("Invalid expression");

    double b = numbers.pop();
    double a = numbers.pop();
    QChar op = operations.pop();

    switch (op.toLatin1()) {
        case '+':
            numbers.push(a + b);
            break;
        case '-':
            numbers.push(a - b);
            break;
        case '*':
            numbers.push(a * b);
            break;
        case '/':
            if (b == 0){
//                QString exptr;
//                exptr =  ui->lineEdit_val->text();
//                ui->lineEdit_val->setText("Division by zero");
//                ui->lineEdit_his->setText(exptr);
                throw std::runtime_error("Division by zero");
            }

            numbers.push(a / b);
            break;
    }
}

int Widget:: precedence(QChar op) {
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == '/')
        return 2;
    return 0;
}
void Widget::on_pushButton_0_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"0");
}

void Widget::on_pushButton_1_clicked()
{
     ui->lineEdit_val->setText(ui->lineEdit_val->text()+"1");
}

void Widget::on_pushButton_29_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"2");
}

void Widget::on_pushButton_30_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"3");
}

void Widget::on_pushButton_21_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"4");
}

void Widget::on_pushButton_22_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"5");
}

void Widget::on_pushButton_23_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"6");
}

void Widget::on_pushButton_24_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"7");
}

void Widget::on_pushButton_25_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"8");
}

void Widget::on_pushButton_26_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"9");

}


void Widget::on_pushButton_add_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"+");
}

void Widget::on_pushButton_sub_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"-");
}

void Widget::on_pushButton_mul_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"*");
}

void Widget::on_pushButton_div_clicked()
{
    ui->lineEdit_val->setText(ui->lineEdit_val->text()+"/");
}

void Widget::on_pushButton_eq_clicked()
{
    QString exptr;
    exptr =  ui->lineEdit_val->text();
    ui->lineEdit_his->setText(exptr);
    double res = evaluateExpression(exptr);
    ui->lineEdit_val->setText(QString::number(res));
}

void Widget::on_pushButton_delAll_clicked()
{
    ui->lineEdit_val->clear();
}

void Widget::on_pushButton_del_clicked()
{
    QString exptr;
    exptr =  ui->lineEdit_val->text();
    int length = exptr.length();
     QString newExptr = exptr.left(length - 1);
     ui->lineEdit_val->setText(newExptr);

}
